package cn.yunsenlin.formula.cloud.group;


import cn.yunsenlin.formula.cloud.auth.AuthRRH;
import cn.yunsenlin.formula.cloud.interfaces.AuthService;
import cn.yunsenlin.formula.cloud.interfaces.GroupService;
import cn.yunsenlin.formula.cloud.mould.ControllerMould;
import cn.yunsenlin.formula.cloud.mysql.bean.generator.TbGroup;
import cn.yunsenlin.formula.cloud.responsibility.ContinueAbleRRH;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/group")
@CrossOrigin
public class GroupController {
    private final AuthRRH authRRH;
    private final GroupUpdateValidRRH groupUpdateValidRRH;
    private final GroupExistRRH groupExistRRH;
    private final GroupService groupService;
    private final AuthService authService;

    public GroupController(AuthRRH authRRH, GroupUpdateValidRRH groupUpdateValidRRH, GroupExistRRH groupExistRRH, GroupService groupService, AuthService authService) {
        this.authRRH = authRRH;
        this.groupUpdateValidRRH = groupUpdateValidRRH;
        this.groupExistRRH = groupExistRRH;
        this.groupService = groupService;
        this.authService = authService;
    }

    /**
     * 添加计算组
     * @param tbGroup 计算组信息
     * @param response response
     * @param session session
     * @return 添加后的计算组
     */
    @RequestMapping(path = "/", method = RequestMethod.PUT)
    public TbGroup addGroup(@RequestBody TbGroup tbGroup, HttpServletResponse response, HttpSession session){
        return new ControllerMould(authRRH,groupExistRRH,new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return groupService.addGroup(tbGroup,authService.selectUserBySession(session).getId());
            }
        }) {
        }.startUpObject(response,session,tbGroup.getParentId());
    }

    /**
     * 删除计算组
     * @param id 计算组ID
     * @param response response
     * @param session session
     */
    @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
    public void removeGroup(@PathVariable("id") Integer id, HttpServletResponse response, HttpSession session){
        new ControllerMould(authRRH,groupExistRRH,new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                groupService.removeGroup(id);
                return null;
            }
        }) {
        }.startUpObject(response,session,id);
    }

    /**
     * 查询根计算组
     * @param response response
     * @param session session
     * @return 根计算组
     */
    @RequestMapping(path = "/root", method = RequestMethod.POST)
    public TbGroup selectRootGroup(HttpServletResponse response, HttpSession session){
        return new ControllerMould(authRRH,new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return groupService.selectRootGroup(authService.selectUserBySession(session).getId());
            }
        }) {
        }.startUpObject(response,session);
    }

    /**
     * 检测计算组名是否重复
     * @param name 计算组名
     * @param parentId 父计算组ID
     * @param response response
     * @param session session
     * @return 是否重复
     */
    @RequestMapping(path = "/checkNameIsRepeat/{parentId}/{name}", method = RequestMethod.POST)
    public boolean checkNameIsRepeat(@PathVariable("name")String name,@PathVariable("parentId")int parentId, HttpServletResponse response, HttpSession session){
        return new ControllerMould(authRRH,groupExistRRH,new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return groupService.checkNameIsRepeat(name, parentId);
            }
        }) {
        }.startUpObject(response,session,parentId);
    }

    /**
     * 查询计算组列表
     * @param parentId 父计算组ID
     * @param response response
     * @param session session
     * @return 计算组列表
     */
    @RequestMapping(path = "/{parentId}", method = RequestMethod.POST)
    public List<TbGroup> selectGroup(@PathVariable("parentId") Integer parentId, HttpServletResponse response, HttpSession session){
        return new ControllerMould(authRRH,groupExistRRH,new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return groupService.selectSonGroup(parentId);
            }
        }) {
        }.startUpArray(response,session,parentId);
    }

    /**
     * 查询计算组面包屑列表
     * @param id 计算组ID
     * @param response response
     * @param session session
     * @return 面包屑列表
     */
    @RequestMapping(path = "/chain/{id}", method = RequestMethod.POST)
    public List<TbGroup> selectChain(@PathVariable("id") Integer id, HttpServletResponse response, HttpSession session){
        return new ControllerMould(authRRH,groupExistRRH,new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return groupService.getChain(id);
            }
        }) {
        }.startUpArray(response,session,id);
    }

    /**
     * 计算组更新信息检查
     * @param tbGroup 计算组更新信息
     * @param response response
     * @param session session
     */
    @RequestMapping(path = "/groupUpdateValidCheck", method = RequestMethod.POST)
    public void groupUpdateValidCheck(@RequestBody TbGroup tbGroup,HttpServletResponse response,HttpSession session){
        new ControllerMould(authRRH,groupUpdateValidRRH) {
        }.startUpObject(response,session,tbGroup);
    }

    /**
     * 修改计算组
     * @param tbGroup 计算组更新信息
     * @param response response
     * @param session session
     */
    @RequestMapping(path = "/", method = RequestMethod.PATCH)
    public void modifyGroup(@RequestBody TbGroup tbGroup,HttpServletResponse response,HttpSession session){
        new ControllerMould(authRRH,groupExistRRH,groupUpdateValidRRH,new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                groupService.updateGroup(tbGroup);
                return null;
            }
        }) {
        }.startUpObject(response,session,tbGroup.getId(),tbGroup);
    }
}
